<?php
namespace app\enterprise\controller;
use app\enterprise\controller\BaseController;
use app\enterprise\service\Radar;
use app\enterprise\service\User;
use think\Cookie;
use think\Request;
use think\Session;

/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/11/20
 * Time: 15:04
 */
class Index extends BaseController
{
    public $appid = '';
    /**
     * 用户登录
     */
    public function login($corpid){
        $url = $this->request->url(true);
        $appid =$this->request->param('corpid');
        $this->appid = $appid;
        $user = new User();
        $user = $user->isLogin($appid,$url);
        if ($user->error_code == 200){
            if ($user->content->ai_power == 'off'){
                $this->error('你没有此应用权限');
            }else{
                $this->redirect("Index/index",['corpid'=>$appid],302);
            }
        }else{
            $this->error('你没有此应用权限');
        }
    }

    /**
     * 进入首页
     * @return mixed
     */
    public function index(){
        $token = Cookie::get('Token');
        $appid =$this->request->param('corpid');
        $url = $this->request->url(true);
        $config = $this->getWxConfig($url,$token,$appid);
        $this->assign('token',$token);
        $this->assign('config',$config);
        $this->assign('corpid',$appid);
        return $this->fetch('index/footer');
    }
    /**
     * ajax请求行为
     * @param $page
     * @param $token
     * @param int $i
     * @return \think\response\Json
     */
    public function getBehaviorList($page){
        $token = Cookie::get('Token');
        $server = new Radar();
        $list = $server->getBehaviorList($page,$token);
        if(empty($list)){
            $data = ['data'=>$list];
        }else{
            $data = ['data'=>$list];
        }
        return json($data);
    }
    /**
     * 行为统计的ajax请求
     * @param $type
     * @param $token
     * @param $timeInfo
     * @return \think\response\Json
     */
    public function getBehaviorCount($type,$timeInfo){
        $token =Cookie::get('Token');
        $server = new Radar();
        $list = $server->getBehaviorCount($type,$token,$timeInfo);
        if (empty($list)){
            $data=['data'=>''];
        }else{
            $data=['data'=>$list];
        }
        return json($data);
    }
    /**
     * 分类页面
     * @return string
     */
    public function gotoCcategory(){
        $data = Request::instance()->param();
        if ($data) {
            $this->assign('type',$data['type']);
            return $this->fetch('radar/seedetails');
        }
    }
    /**
     * 行为分类的ajax请求
     * @param $type
     * @param $token
     * @param $timeInfo
     * @return \think\response\Json
     */
    public function getBehaviorByCategory($type,$timeInfo,$page){
        $token =Cookie::get('Token');
        $server = new Radar();
        $list = $server->getBehaviorByCategory($type,$token,$timeInfo,$page);
        if (empty($list)){
            $data=['data'=>''];
        }else{
            $data=['data'=>$list];
        }
        return json($data);
    }
    /**
     * 用户行为页面
     * @return mixed
     */
    public function  gotoUserCount(){
        $data = Request::instance()->param();
        if ($data) {
            $this->assign('user_id',$data['user_id']);
            return $this->fetch('radar/seePeoper');
        }
    }
    /**
     * 获取用户列表
     * @param $token
     * @return \think\response\Json
     */
    public function getCustoms($timeInfo,$page,$size){
        $token =Cookie::get('Token');
        $server = new Radar();
        $list =  $server->getCustoms($timeInfo,$page,$size,$token);
        if (empty($list)){
            $data=['data'=>''];
        }else{
            $data=['data'=>$list];
        }
        return json($data);
    }
    /**
     * 根据用户id获取行为
     * @param $token
     * @return \think\response\Json
     */
    public function getCustomById($user_id,$timeInfo){
        $token = Cookie::get('Token');
        $server = new Radar();
        $list =  $server->getCustomsById($user_id,$timeInfo,$token);
        if (empty($list)){
            $data=['data'=>''];
        }else{
            $data=['data'=>$list];
        }
        return json($data);
    }

    /**
     * 获取排名信息
     * @param $time
     * @param $page
     * @param $size
     * @return \think\response\Json
     */
    public function getRank($time,$page,$size){
        $token = Cookie::get('Token');
        $server = new Radar();
        $list =  $server->getRanke($time,$page,$size,$token);
        if (empty($list)){
            $data=['data'=>''];
        }else{
            $data=['data'=>$list];
        }
        return json($data);
    }
    /**
     * 获取我的排名信息
     * @param $time
     * @param $page
     * @param $size
     * @return \think\response\Json
     */
    public function getMyRank($time){
        $token = Cookie::get('Token');
        $server = new Radar();
        $list =  $server->getMyRanke($time,$token);
        if (empty($list)){
            $data=['data'=>''];
        }else{
            $data=['data'=>$list];
        }
        return json($data);
    }

    /**
 * 用户点赞
 * @param $id
 * @return \think\response\Json
 */
    public function createThumbsup($id){
        $token = Cookie::get('Token');
        $server = new Radar();
        $list =  $server->createThumbsup($id,$token);
        if (empty($list)){
            $data=['data'=>''];
        }else{
            $data=['data'=>$list];
        }
        return json($data);
    }

    /**
     * 点赞列表
     * @param $id
     * @return \think\response\Json
     */
    public function getThumbsupList($id,$size,$page){
        $token = Cookie::get('Token');
        $server = new Radar();
        $list =  $server->getThumbsupList($id,$size,$page,$token);
        if (empty($list)){
            $data=['data'=>''];
        }else{
            $data=['data'=>$list];
        }
        return json($data);
    }

}